package org.bsc.mgwt.client.widget;

import java.util.Iterator;

import org.bsc.mgwt.client.MobileResourceAware;

import com.google.gwt.dom.client.Document;
import com.google.gwt.user.client.ui.ComplexPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.Widget;
import com.googlecode.mgwt.ui.client.theme.base.ListCss;
import com.googlecode.mgwt.ui.client.widget.WidgetList;

public class EditableWidgetList extends WidgetList implements MobileResourceAware {
	
	private static class WidgetListEntry extends Entry {
		
		private static class LIFlowPanel extends ComplexPanel {

			public LIFlowPanel() {
				setElement(Document.get().createLIElement());
			}

			@Override
			public void add(Widget w) {
				add(w, getElement());
			}
		}

		private Panel container;

		public WidgetListEntry(ListCss css) {
			container = new LIFlowPanel();
			
			mobileBundle.editableWidgetList().ensureInjected();
			
			container.addStyleName(mobileBundle.editableWidgetList().list());
			final Image image = new Image( mobileBundle.editSmallImage().getSafeUri());
			image.setStyleName( mobileBundle.editableWidgetList().icon() );
			container.add( image );
			
			initWidget(container);
		}

		@Override
		public void add(Widget w) {
			container.add(w);
		}

		@Override
		public void clear() {
			container.clear();
		}

		@Override
		public Iterator<Widget> iterator() {
			return container.iterator();
		}

		@Override
		public boolean remove(Widget w) {
			return container.remove(w);
		}
	}

	public EditableWidgetList() {
		super();
	}

	public EditableWidgetList(ListCss css) {
		super(css);
	}

	@Override
	protected Entry createEntry(ListCss css) {
		WidgetListEntry e =  new WidgetListEntry(css);
		
		return e;
	}

}
